package com.atguigu.tingshu.comment.config;

import com.tencentcloudapi.common.Credential;
import lombok.Data;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.ConfigurationProperties;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.tms.v20201229.TmsClient;
import com.tencentcloudapi.tms.v20201229.models.*;
import org.springframework.stereotype.Component;

import java.nio.charset.StandardCharsets;
import java.util.Base64;



/***
 * 评论文本配置初始化
 */
@Component
@ConfigurationProperties(prefix = "tencentcloud")
@Data
public class TextDetection {
    private String secretId;
    private String secretKey;

    public JSONObject greenTextDetection(String text) throws TencentCloudSDKException {
        // 实例化认证对
        Credential cred = new Credential(secretId, secretKey);
        // 实例化要请求产品的client对象
        TmsClient client = new TmsClient(cred, "ap-guangzhou");
        // 实例化请求对象,每个接口都会对应一个request对象
        TextModerationRequest req = new TextModerationRequest();

        //Base64加密
        String encryptionText = Base64.getEncoder().encodeToString(text.getBytes(StandardCharsets.UTF_8));
        //设置内容参数
        req.setContent(encryptionText);

        // 返回的resp是一个TextModerationResponse的实例，与请求对象对应
        TextModerationResponse resp = client.TextModeration(req);

        // 输出json格式的字符串回包
        String result = TextModerationResponse.toJsonString(resp);
        return JSON.parseObject(result);
    }

}
